<template>
  <div class="RecentNews">
    <div class="article">
      <div class="t1">近期文章</div>
      <ul class="list">
        <li v-for="(item,index) in newsList.slice(0,5)" :key="index" @click="checkDetail(item)">{{ item.title }}</li>
        <li v-if="newsList.length == 0">没有文章可显示</li>
      </ul>
    </div>
    <div class="article">
      <div class="t1">近期评论</div>
      <ul class="list">
        <li>没有评论可显示</li>
      </ul>
    </div>
  </div>
</template>

<script>
import { getNewsList } from "@/api/configApi.js";
export default {
  name: "RecentNews",
  data() {
    return {
        newsList: []
    };
  },
  mounted() {
    this.initial();
  },
  methods: {
    async initial() {
      let result = await getNewsList();
      if (result.code == 200) {
        this.newsList = result.data.data;
      }
    },
    checkDetail(item) {
      sessionStorage.setItem('newsDetail', JSON.stringify(item));
      if(this.$route.path == '/newsDetail'){
        this.$router.go();
      }else{
        this.$router.push("/newsDetail");
      }
      document.documentElement.scrollTop = 0;
    }
  }
};
</script>

<style lang="scss" scoped>
.RecentNews {
  width: 100%;
  .article {
    margin-bottom: 30px;
    .t1 {
      font-weight: bold;
      margin: 20px 0;
    }
    .list {
      list-style: none;
      li {
        color: #666666;
        font-size: 14px;
        margin: 15px 0;
        cursor: pointer;
      }
    }
  }
}
</style>
